
import java.util.*;

public class e2piecelist {
	int count;
    e2piece first;
    e2piece current;
    e2piece next;
    LinkedList list;
    boolean lock;
    ListIterator currentit;

    public e2piecelist() {
        list = new LinkedList();
        currentit = list.listIterator();
        count = 0;
        lock = false;
    }

    public boolean shuffle() {
    	if (lock == true) {
        	//System.out.println("waiting lock in : shuffle");
    	}
    	while (lock == true) {
    		//wait for lock to be removed
    	}
    	//System.out.println("lock released in : shuffle");
    	lock = true;
        Collections.shuffle(list, new Random());
        lock = false;
        return true;
    }

    public boolean hasNext() {
    	boolean result;
    	result = currentit.hasNext();
    	if (result == false) {
    		lock = false;    		
    	} else {
    		lock = true;
    	}
        return result;

    }

    public e2piece next() {
        return (e2piece) currentit.next();
    }

    public boolean restart() {
    	if (lock == true) {
        	//System.out.println("waiting lock in : restart");
    	}
    	while (lock == true) {
    		//wait for lock to be removed
    	}
    	//System.out.println("lock released in : restart");
    	lock = true;
        currentit = list.listIterator();
        return true;
    }

    public boolean add_piece(e2piece in_piece) {
    	if (lock == true) {
        	//System.out.println("waiting lock in : add_piece");
    	}
    	while (lock == true) {
    		//wait for lock to be removed
    	}
    	//System.out.println("lock released in : add_piece");
    	lock = true;
        list.add(in_piece);
        count++;
        currentit = list.listIterator();
        //currentit.add(in_piece);
    	lock = false;
        return true;
    }

    public boolean remove_piece(int in_id) {
    	if (lock == true) {
        	//System.out.println("waiting lock in : remove_piece");
    	}
    	while (lock == true) {
    		//wait for lock to be removed
    	}
    	//System.out.println("lock released in : remove_piece");
    	lock = true;
        ListIterator tempit = list.listIterator();
        boolean result = false;
        e2piece temppiece;
        while (tempit.hasNext()) {
            temppiece = (e2piece) tempit.next();
            if (temppiece.id == in_id) {
                tempit.remove();
                count--;
                result = true;
                break;
            }

        }
        currentit = list.listIterator();
        lock = false;
        return result;
    }
    
    public int get_number_of_elements() {
    	return count;
    }

    public boolean is_piece_in_list(int in_id) {
    	if (lock == true) {
        	//System.out.println("waiting lock in : is_piece_in_list");
    	}
    	while (lock == true) {
    		//wait for lock to be removed
    	}
    	//System.out.println("lock released in : is_piece_in_list");
    	lock = true;
        ListIterator tempit = list.listIterator();
        boolean result = false;
        e2piece temppiece;
        while (tempit.hasNext()) {
            temppiece = (e2piece) tempit.next();
            if (temppiece.id == in_id) {
                result = true;
                break;
            }

        }
        lock = false;
        return result;
    }

    public e2piece get_piece(int in_id) {
    	if (lock == true) {
        	//System.out.println("waiting lock in : get_piece");
    	}
    	while (lock == true) {
    		//wait for lock to be removed
    	}
    	lock = true;
    	//System.out.println("lock released in : get_piece");
    	//System.out.println("    query piece "+in_id+" pointer");
        e2piece returned_piece = null;
        ListIterator tempit = list.listIterator();
        e2piece temppiece;
        while (tempit.hasNext()) {
            temppiece = (e2piece) tempit.next();
        	//System.out.println("     comparison "+in_id+" vs "+temppiece.id);
            if (temppiece.id == in_id) {
                tempit.remove();
                returned_piece = temppiece;
                break;
            }

        }
        lock = false;
        return returned_piece;
    }
}
